/* CLIENT.C */

/*
	Copyright (C) 2010 by Lesnikov Andrey <ozkriff@gmail.com>
	This file is part of ozkriff_game_2.

	ozkriff_game_2 is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	ozkriff_game_2 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with ozkriff_game_2.  If not, see <http://www.gnu.org/licenses/>.

	See the COPYING file for more details.
*/

//#include "GL/glew.h"
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_opengl.h"
#include "SDL/SDL_net.h"

#include "defines.h"

#include "camera.h"
#include "nograph.h"
#include "draw.h"
#include "gui.h"
#include "picking.h"
#include "path.h"


extern int	video_flags;
extern int	cursorX;
extern int	cursorY;
extern int	PREVIOUS_MODE;
extern bool	CANCAMROT;
extern uint	DRAW_ALL_PATH;
extern int	PICKED_SMTH;
extern SDL_Surface *
		surface;
extern int	SCR_WIDTH;
extern int	SCR_HEIGHT;
extern int	SCR_BPP;
extern GLUquadricObj *
		GQO;
extern camera	CAMERA;
extern uint	PLAYER;
extern int	MODE;
extern cell	MAP[MAP_SIZE_X][MAP_SIZE_Y];
extern uint	aX;
extern uint	aY;
extern bool	IS_PROGRAM_LOOPING;
extern char *	SERVER;
extern unit *	SELECTED_UNIT;





// astyle -SUET81

void
keys(SDL_Event E)
{
	switch (E.key.keysym.sym)
	{
		default:
			break;
		case SDLK_q:
		case SDLK_ESCAPE:
			IS_PROGRAM_LOOPING = false;
			break;
		case SDLK_RETURN:
		case SDLK_SPACE:
			end_player_turn();
			break;
		case SDLK_UP:
			CAMERA.y--;
			break;
		case SDLK_DOWN:
			CAMERA.y++;
			break;
		case SDLK_LEFT:
			CAMERA.x++;
			break;
		case SDLK_RIGHT:
			CAMERA.x--;
			break;
		case SDLK_p:
			if (MODE != MODE_PAUSE) {
				PREVIOUS_MODE = MODE;
				MODE = MODE_PAUSE;
			} else {
				MODE = PREVIOUS_MODE;
			}
			break;
		case SDLK_c:
			CANCAMROT = !CANCAMROT;
			break;
		case SDLK_m:
			DRAW_ALL_PATH++;
			if(DRAW_ALL_PATH > 2)
				DRAW_ALL_PATH = 0;
			break;
		case SDLK_w:
		{
			if(PICKED_SMTH)
			{
				MAP[aX][aY].type++;
				if(MAP[aX][aY].type > 3)
					MAP[aX][aY].type = 0;
				if(SELECTED_UNIT)
					fill_map(SELECTED_UNIT);
				
				send_to_server(NET_CH_TERRAIN);
				send_to_server(aX);
				send_to_server(aY);
			}
			break;
		}
	}
}

void
hanlde_events()
{
	SDL_Event E;

	while(SDL_PollEvent(&E)) switch(E.type)
	{
		default:
			//puts("unknown event!");
			break;
			
		case SDL_QUIT:
			IS_PROGRAM_LOOPING = false;
			break;
			
		case SDL_VIDEORESIZE:
			SDL_SetVideoMode(E.resize.w, E.resize.h, 32, video_flags);
			reshape(E.resize.w, E.resize.h);
			break;
			
		case SDL_MOUSEMOTION:
			cursorX = E.motion.x;
			cursorY = E.motion.y;
			
			if (CAMERA.is_rotating) {
				CAMERA.horiz_angle += E.motion.xrel;
				CAMERA.vert_angle += E.motion.yrel;
#if(1) //ограничивает вращение камеры
				if (CAMERA.vert_angle < 30 || CAMERA.vert_angle > 89)
					CAMERA.vert_angle -= E.motion.yrel;
				if (CAMERA.horiz_angle > 360 || CAMERA.horiz_angle < -360)
					CAMERA.horiz_angle = 0;
#endif
			}
			break;
			
		case SDL_MOUSEBUTTONDOWN:
			if (E.button.button == SDL_BUTTON_RIGHT) {
				CAMERA.is_rotating = true;
			}
			if (E.button.button == SDL_BUTTON_LEFT)
				handle_mouse_events(E);
			break;
			
		case SDL_MOUSEBUTTONUP:
			if (E.button.button == SDL_BUTTON_RIGHT) {
				CAMERA.is_rotating = false;
			} else {
				if (E.button.button == SDL_BUTTON_WHEELUP   && CAMERA.distanse < 25)
					CAMERA.distanse++;
				if (E.button.button == SDL_BUTTON_WHEELDOWN && CAMERA.distanse > 2)
					CAMERA.distanse--;
			}
			// если это не закоментить - то обработка вызыывается дважды за клик.
			//if (E.button.button == SDL_BUTTON_LEFT) handle_mouse_events(E);
			
			break;
			
			
		case SDL_KEYDOWN:
			keys(E);
	}
}



void
init_sdl()
{
	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
	atexit(SDL_Quit);
	
	/* the flags to pass to SDL_SetVideoMode */
	video_flags  = SDL_OPENGL;
	video_flags |= SDL_GL_DOUBLEBUFFER;
	video_flags |= SDL_RESIZABLE;
	
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	/* get a SDL surface */
	surface = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT,
				   SCR_BPP, video_flags);
	SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 0, 0, 0, 0));
	
	reshape(SCR_WIDTH, SCR_HEIGHT);
}



void
init_all()
{
	srand(time(0));

	init_net();
	init_sdl();
	init_opengl();
	init_q();
	init_models();
	init_textures();
	init_interface();
	generate_hex();
	init_map();
	init_camera();

	GQO = gluNewQuadric();
}



#undef main //write straight to the cosole

int
main(int argc, char **argv)
{
	if(argc != 2 && argc != 3)
	{
		puts("wrong argc. <playerID> [IP]");
		exit(0);
	}
	PLAYER = atoi(argv[1]);
	if(argc == 3)
		SERVER = argv[2];

	init_all();

	//puts((char*)glGetString(GL_VERSION)); 
	
	while(IS_PROGRAM_LOOPING)
	{
		do_net();
		hanlde_events();
		do_picking();
		draw();
		do_logic();
		camera_logic();
		change_frames();
	}
	
	send_to_server(NET_QUIT);
	
	return(0);
}
